package controllers

import (
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/validation"
	"teamcoop/action"
	. "teamcoop/global"
)

type RegisterController struct {
	beego.Controller
}

func (this *RegisterController) Get() {
	this.TplNames = "register.html"
	this.Data["error_infoamation"] = ""
	this.Data["error_hide"] = "hide"
}

func (this *RegisterController) Post() {
	var err error = nil
	this.TplNames = "register.html"
	valid := validation.Validation{}
	rigster := &FormRigster{}
	err = this.ParseForm(rigster)
	beego.Info("receive:register\t", rigster)
	if err != nil {
		goto _error
	}
	_, err = valid.Valid(rigster)
	if err != nil {
		goto _error
	}
	_, err = action.UserRegister(rigster.Email, rigster.Nick, rigster.Pwd, 0)
	if err == nil {

		this.Ctx.WriteString(`<script lang="javascript">
			alert("注册成功!\n点击确定自动跳转到主页面!");
			window.location.href = "/"
			</script>'`)

	}
	return
_error:
	if err != nil {
		this.Data["error_infoamation"] = err.Error()
	}

}
